//
//  MasterListViewController.m
//  DaleTou
//
//  Created by  on 2018/5/28.
//  Copyright © 2018年 geren. All rights reserved.
//

#import "MasterListViewController.h"
#import "MaterListTableCell.h"
#import "MasterTableHeader.h"
#import "MasterViewController.h"
#import "MJExtension.h"
#import "MasterModel.h"
#import "Appearance.h"
#import "DLTModel.h"
#import <MJRefresh.h>

@interface MasterListViewController ()<UITableViewDelegate,UITableViewDataSource> {
    NSMutableArray *_dataArray;
    NSArray *_allList;
}

@property (nonatomic,strong) UITableView *tableView;
@property(nonatomic,assign)NSInteger currentindex;

@end

@implementation MasterListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initData];
    [self.view addSubview:self.tableView];
}

- (void)initData {
    self.tableView.tableFooterView.hidden = NO;
    _allList = [MasterModel mj_objectArrayWithFilename:@"masterList.plist"];

    NSArray *arr = [_allList[self.masterType] subarrayWithRange:NSMakeRange(0, 10)];
    _dataArray = [arr mutableCopy];
     self.tableView.mj_footer.hidden = NO;
}

- (void)loadMore{
    NSArray *arr = [_allList[self.masterType] subarrayWithRange:NSMakeRange(10, 3)];
    [_dataArray addObjectsFromArray:arr];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self.tableView reloadData];
        self.tableView.mj_footer.hidden = YES;
        [self.tableView.mj_footer endRefreshing];
    });

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _dataArray.count;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString * const idS = @"MaterListTableCell";
    MaterListTableCell *artCell = [tableView dequeueReusableCellWithIdentifier:idS];
    if (!artCell) {
        artCell = [MaterListTableCell defaultClassNameNibView];
    }
    artCell.index = indexPath.row;
    artCell.model = _dataArray[indexPath.row];
    return artCell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 80;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 55;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    MasterTableHeader *header = [MasterTableHeader defaultClassNameNibView];
    for (UIButton *btn in header.btnArray) {
        NSInteger tag = btn.tag;
        btn.layer.cornerRadius = 12.5f;
        btn.layer.masksToBounds = YES;
        if (tag == self.currentindex) {
            [btn setTitleColor:[UIColor colorWithHexString:@"ffffff"] forState:UIControlStateNormal];
            [btn setBackgroundColor:[UIColor colorWithHexString:@"F57321"]];
            btn.layer.borderColor = [UIColor colorWithHexString:@"ffffff"].CGColor;
            btn.layer.borderWidth =  1.f;
        }else{
            [btn setTitleColor:[UIColor colorWithHexString:@"333333"] forState:UIControlStateNormal];
            [btn setBackgroundColor:[UIColor colorWithHexString:@"ffffff"]];
            btn.layer.borderColor = [UIColor colorWithHexString:@"999999"].CGColor;
            btn.layer.borderWidth = 1.f;
        }
    }
    header.selcetpressed = ^(NSInteger index) {
        if (index != self.currentindex) {
            self.currentindex = index;
//            _dataArray = _allList[index];
            self.masterType = index;
            [self initData];
            [self.tableView reloadData];
        }
    };
    return header;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    MasterViewController *vc = [[MasterViewController alloc] init];
    vc.masterInfo = _dataArray[indexPath.row];
    vc.index = self.currentindex;
    vc.pastData = _openList;
    [self.navigationController pushViewController:vc animated:YES];
}

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, NAV_HEIGHT, Main_Screen_Width, Main_Screen_Height - NAV_HEIGHT) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMore)];
        _tableView.tableFooterView = [UIView new];
    }
    return _tableView;
}

@end
